<?php

namespace App\Listeners;

use App\Http\Controllers\Miniprograme\SubscribeController;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Facades\Log;
use Modules\Manager\Entities\Department;
use Modules\Manager\Entities\Manager;

class TradeCreateListener
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  object  $event
     * @return void
     */
    public function handle($event)
    {

        if($event->type==config('wechat.notify.mini.TradeCreate')){
            $order = $event->order;
            // $name =   Department::query()->where('id',$order->department)->value('name');
            $array =  Manager::query()->where('department_id',$order->department)
                ->where('post_id',2)
                ->whereNotNull('openid')
                ->pluck('openid');
            // 现在只通知对应店长了，不通知管理员
            // $manager =  Manager::query()
            //     ->where('post_id',1)
            //     ->whereNotNull('openid')
            //     ->pluck('openid');
            foreach($array as $openid){
                if ($openid) {
                    (new SubscribeController())->manager($openid,$order);
                }
            }
            // foreach($manager as $openids){
            //     (new SubscribeController())->manager($openids,$order);
            // }
        }
    }
}
